package com.my.model;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.Version;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * <p>
 * 
 * </p>
 *
 * @author wxq
 * @since 2020-06-13
 */

@EqualsAndHashCode(callSuper = false)
@ApiModel(value="User对象", description="权限类")
@AllArgsConstructor
@NoArgsConstructor
public class User implements UserDetails, Serializable{

    private static final long serialVersionUID=1L;
    @ApiModelProperty(value = "id")
//    @TableId(value = "id", type = IdType.ID_WORKER)
    private Long id;

    @ApiModelProperty(value = "用户名")
    private String userName;

    @ApiModelProperty(value = "是否可用1为可用0不可用")
    private Integer enabled;

    @ApiModelProperty(value = "密码")
    private String passsword;

    @ApiModelProperty(value = "是否锁定1为锁定0未锁定")
    private Integer locked;

    private List<Role> roles;

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public void setUserName(String userName) {
        this.userName = userName;
    }



    public void setEnabled(Integer enabled) {
        this.enabled = enabled;
    }



    public void setPasssword(String passsword) {
        this.passsword = passsword;
    }



    public void setLocked(Integer locked) {
        this.locked = locked;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        for(Role role:roles){
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        return authorities;
    }
    @JsonIgnore
    @Override
    public String getPassword() {
        return passsword;
    }

    @Override
    public String getUsername() {
        return userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        boolean i=true;
        if (locked==null);
        locked=0;
        if(locked==1)
            i=false;
        return i;

    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        boolean i=true;
        if(enabled==null)
            enabled=1;
        if(enabled==0)
            i=false;
        return i;
    }
}
